<template>
  <view class="jnpf-wrap jnpf-wrap-workflow">
    <u-form
      :model="dataForm"
      ref="dataForm"
      :errorType="['toast']"
      label-position="left"
      label-width="150"
      label-align="left"
    >
      <u-form-item label="单别" v-if="judgeShow('ca001')">
        <u-input v-model="dataForm.ca001" placeholder="单别" disabled></u-input>
      </u-form-item>
      <u-form-item label="单号" v-if="judgeShow('ca002')">
        <u-input v-model="dataForm.ca002" placeholder="单号" disabled></u-input>
      </u-form-item>
      <u-form-item label="单据日期" v-if="judgeShow('ca018')">
        <u-input
          v-model="dataForm.ca018"
          placeholder="单据日期"
          disabled
        ></u-input>
      </u-form-item>
      <u-form-item label="主件品号" v-if="judgeShow('ca003')">
        <u-input
          v-model="dataForm.ca003"
          placeholder="主件品号"
          disabled
        ></u-input>
      </u-form-item>
      <u-form-item label="品名" v-if="judgeShow('mb002')">
        <u-input
          v-model="dataForm.mb002"
          placeholder="品名"
          :disabled="judgeWrite('mb002')"
        ></u-input>
      </u-form-item>
      <u-form-item label="规格" v-if="judgeShow('mb003')">
        <u-input
          v-model="dataForm.mb003"
          placeholder="规格"
          :disabled="judgeWrite('mb003')"
        ></u-input>
      </u-form-item>
      <u-form-item label="属性" v-if="judgeShow('mb025')">
        <u-input
          v-model="dataForm.mb025"
          placeholder="属性"
          :disabled="judgeWrite('mb025')"
        ></u-input>
      </u-form-item>
      <u-form-item label="单位" v-if="judgeShow('ca004')">
        <u-input
          v-model="dataForm.ca004"
          placeholder="单位"
          :disabled="judgeWrite('ca004')"
        ></u-input>
      </u-form-item>
      <u-form-item label="标准批量" v-if="judgeShow('ca006')">
        <u-input
          v-model="dataForm.ca006"
          placeholder="标准批量"
          :disabled="judgeWrite('ca006')"
        ></u-input>
      </u-form-item>
      <u-form-item label="机台吨位" v-if="judgeShow('ca012')">
        <u-input
          v-model="dataForm.ca012"
          placeholder="机台吨位"
          :disabled="judgeWrite('ca012')"
        ></u-input>
      </u-form-item>
      <u-form-item label="审核日期" v-if="judgeShow('ca022')">
        <u-input
          v-model="dataForm.ca022"
          placeholder="审核日期"
          :disabled="judgeWrite('ca022')"
        ></u-input>
      </u-form-item>
      <u-form-item label="审核者" v-if="judgeShow('ca016')">
        <u-input
          v-model="dataForm.ca016"
          placeholder="审核者"
          :disabled="judgeWrite('ca016')"
        ></u-input>
      </u-form-item>
      <u-form-item label="子件关系" v-if="judgeShow('ca013')">
        <view
          v-for="(item, index) in ca013Options"
          :key="index"
          style="margin-left: 5px"
        >
          <u-tag :text="item" type="success" />
        </view>
      </u-form-item>
      <u-form-item label="工单单别" v-if="judgeShow('ca007')">
        <u-input
          v-model="dataForm.ca007"
          placeholder="工单单别"
          :disabled="judgeWrite('ca007')"
        ></u-input>
      </u-form-item>
      <u-form-item label="签核状态码" v-if="judgeShow('ca020')">
        <view
          v-for="(item, index) in ca020Options"
          :key="index"
          style="margin-left: 5px"
        >
          <u-tag :text="item" type="success" />
        </view>
      </u-form-item>
      <view class="jnpf-table" style="margin-top: 24rpx">
        <view
          class="jnpf-table-item"
          style="margin-bottom: 24rpx"
          v-for="(item, i) in dataForm.bomcbList"
          :key="i"
        >
          <u-form-item label="序号" style="background-color: aqua">
            {{ i + 1 }}
          </u-form-item>
          <u-form-item label="元件品号" v-if="judgeShow('bomcbList-cb005')">
            <u-input
              v-model="item.cb005"
              placeholder="元件品号"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="品号" v-if="judgeShow('bomcbList-mb002')">
            <u-input v-model="item.mb002" placeholder="品号" disabled></u-input>
          </u-form-item>
          <u-form-item label="规格" v-if="judgeShow('bomcbList-mb003')">
            <u-input v-model="item.mb003" placeholder="规格" disabled></u-input>
          </u-form-item>
          <u-form-item label="单位" v-if="judgeShow('bomcbList-cb006')">
            <u-input v-model="item.cb006" placeholder="单位" disabled></u-input>
          </u-form-item>
          <u-form-item label="属性" v-if="judgeShow('bomcbList-cb005MB025')">
            <u-input
              v-model="item.cb005MB025"
              placeholder="属性"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="组成用量" v-if="judgeShow('bomcbList-cb008')">
            <u-input
              v-model="item.cb008"
              placeholder="组成用量"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="底数" v-if="judgeShow('bomcbList-cb009')">
            <u-input v-model="item.cb009" placeholder="底数" disabled></u-input>
          </u-form-item>
          <u-form-item label="损耗率" v-if="judgeShow('bomcbList-cb010')">
            <u-input
              v-model="item.cb010"
              placeholder="损耗率"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="生效日期" v-if="judgeShow('bomcbList-cb013')">
            <u-input
              v-model="item.cb013"
              placeholder="生效日期"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="失效日期" v-if="judgeShow('bomcbList-cb014')">
            <u-input
              v-model="item.cb014"
              placeholder="失效日期"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="材料类型" v-if="judgeShow('bomcbList-cb019')">
            <u-input
              v-model="item.cb019"
              placeholder="材料类型"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="交叉排程" v-if="judgeShow('bomcbList-cb032')">
            <u-input
              v-model="item.cb032"
              placeholder="交叉排程"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item
            label="考虑上阶固定前置天数"
            v-if="judgeShow('bomcbList-cb023')"
          >
            <u-input
              v-model="item.cb023"
              placeholder="考虑上阶固定前置天数"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="工艺" v-if="judgeShow('bomcbList-cb011')">
            <u-input v-model="item.cb011" placeholder="工艺" disabled></u-input>
          </u-form-item>
          <u-form-item label="工艺名称" v-if="judgeShow('bomcbList-mw002')">
            <u-input
              v-model="item.mw002"
              placeholder="工艺名称"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="投料间距" v-if="judgeShow('bomcbList-cb020')">
            <u-input
              v-model="item.cb020"
              placeholder="投料间距"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="默认选择" v-if="judgeShow('bomcbList-cb015')">
            <u-input
              v-model="item.cb015"
              placeholder="默认选择"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="标准成本计算" v-if="judgeShow('bomcbList-cb016')">
            <u-input
              v-model="item.cb016"
              placeholder="标准成本计算"
              disabled
            ></u-input>
          </u-form-item>

          <u-form-item label="分量损耗" v-if="judgeShow('bomcbList-cb031')">
            <u-input
              v-model="item.cb031"
              placeholder="分量损耗"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="固定损耗量" v-if="judgeShow('bomcbList-cb030')">
            <u-input
              v-model="item.cb030"
              placeholder="固定损耗量"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="备注" v-if="judgeShow('bomcbList-cb018')">
            <u-input v-model="item.cb018" placeholder="备注" disabled></u-input>
          </u-form-item>
          <u-form-item label="组合替代" v-if="judgeShow('bomcbList-cb029')">
            <u-input
              v-model="item.cb029"
              placeholder="组合替代"
              disabled
            ></u-input>
          </u-form-item>
          <u-form-item label="插件位置" v-if="judgeShow('bomcbList-cb017')">
            <u-input
              v-model="item.cb017"
              placeholder="插件位置"
              disabled
            ></u-input>
          </u-form-item>
        </view>
      </view>
    </u-form>
  </view>
</template>

<script>
import comMixin from "../mixin";
import {
  getGoodsList,
  getCustomerList,
  Info,
  Create,
  Update,
} from "@/api/apply/order";
import { getBomca } from "@/api/workFlow/flowBefore";
import {
  getDictionaryDataSelector,
  getDataInterfaceDataInfo,
} from "@/api/common";
export default {
  mixins: [comMixin],
  data() {
    return {
      formOperatesList: [],
      dataForm: {},
      id: "",
      ca020Options: [],
      ca013Options: [],
      options: [
        { fullName: "必选，唯一", id: "1" },
        { fullName: "必选，至少一个", id: "2" },
        { fullName: "可选，唯一", id: "3" },
        { fullName: "任意选", id: "4" },
      ],
    };
  },
  methods: {
    init(data, id, flowFormContentJson) {
      this.setting = data;
      this.formOperatesList = data.formOperates;
      this.id = data.id;
      this.getInfoFn(this.id);
    },
    getInfoFn(id) {
      let query = {
        id: id,
      };
      getBomca(query).then((res) => {
        if (res.code == 200) {
          this.dataForm = res.data;
          this.getca020Options();
        }
      });
    },

    //初始化材料类型
    getcb019Options() {
      getDictionaryDataSelector("413503187920706117").then((res) => {
        this.cb019Options = res.data.list;
      });
    },
    //初始化签核状态码
    getca020Options() {
      getDictionaryDataSelector("393566623006470277").then((res) => {
        this.ca020Options = res.data.list
          .filter((item) => this.dataForm.ca020.includes(item.enCode))
          .map((item) => item.fullName);
        this.ca013Options = this.options
          .filter((item) => this.dataForm.ca013.includes(item.id))
          .map((item) => item.fullName);
      });
    },

    selfGetInfo() {
      Info(this.setting.id).then((res) => {
        this.dataForm = res.data;
        if (this.dataForm.fileJson) {
          this.fileList = JSON.parse(this.dataForm.fileJson);
        }
      });
    },
    selfSubmit() {
      this.dataForm.status = this.eventType === "submit" ? 0 : 1;
      if (this.eventType === "save") return this.selfHandleRequest();
      uni.showModal({
        title: "提示",
        content: "您确定要提交当前流程吗？",
        success: (res) => {
          if (res.confirm) {
            this.selfHandleRequest();
          }
        },
      });
    },
    selfHandleRequest() {
      if (!this.dataForm.id) delete this.dataForm.id;
      const formMethod = this.dataForm.id ? Update : Create;
      formMethod(this.dataForm).then((res) => {
        uni.showToast({
          title: res.msg,
          complete: () => {
            setTimeout(() => {
              uni.$emit("refresh");
              uni.navigateBack();
            }, 1500);
          },
        });
      });
    },
    initData() {
      getGoodsList().then((res) => {
        this.goodsOptions = res.data.list;
      });
      getCustomerList().then((res) => {
        this.customerOptions = res.data.list;
      });
    },
    selfInit() {
      this.addGoods();
      this.addPlan();
    },
    onChange(e) {
      const user = e[0];
      this.dataForm.salesmanName = user.fullName;
    },
    addGoods() {
      const item = {
        goodsId: "",
        goodsCode: "",
        goodsName: "",
        specifications: "",
        unit: "",
        qty: "",
        price: "",
        amount: "",
        discount: null,
        cess: null,
        actualPrice: "",
        actualAmount: "",
        description: "",
      };
      this.dataForm.goodsList.push(item);
    },
    delGoods(index) {
      this.dataForm.goodsList.splice(index, 1);
    },
    addPlan() {
      const item = {
        receivableDate: "",
        receivableRate: "",
        receivableMoney: "",
        receivableMode: "",
        abstract: "",
      };
      this.dataForm.collectionPlanList.push(item);
    },
    delPlan(index) {
      this.dataForm.collectionPlanList.splice(index, 1);
    },
    onGoodsChange(e, i) {
      const goods = e[0];
      this.dataForm.goodsList[i] = {
        goodsId: goods.id,
        goodsCode: goods.code,
        goodsName: goods.text,
        specifications: goods.specifications,
        unit: goods.unit,
        qty: 1,
        price: goods.price,
        amount: goods.price,
        discount: 100,
        cess: 0,
        actualPrice: goods.price,
        actualAmount: goods.price,
        description: "",
      };
      this.$forceUpdate();
    },
    onCustomerChange(e) {
      const customer = e[0];
      this.dataForm.customerName = customer.text;
    },
    count(row) {
      //金额 = 数量*单价
      row.amount = this.jnpf.toDecimal(
        parseFloat(row.price) * parseFloat(row.qty)
      );
      //折扣价 = (单价*折扣)
      var discountPrice = row.price * (row.discount / 100);
      //实际单价 = 折扣价 * (1 + (税率 / 100))
      row.actualPrice = this.jnpf.toDecimal(
        discountPrice * (1 + row.cess / 100)
      );
      //实际金额
      row.actualAmount = this.jnpf.toDecimal(
        parseFloat(row.actualPrice) * parseFloat(row.qty)
      );
      this.$forceUpdate();
    },
  },
};
</script>
